// subpkg/search/search.js
import {searchMovie} from '../../api/movie'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    movieList: []
  },
  search(e) {
    const value = e.detail.value
    clearTimeout(this.timer)
    this.timer = setTimeout(() => {
        this.onSearch(value)
    }, 800);
  },
  async onSearch(value) {
    wx.showLoading({
      title: '搜索中...',
    })
    const res = await searchMovie({
      keyword: value
    })
    if (!res || res.length === 0) return
    // 处理数据
    res.forEach(item => {
      item.movieInfo = {
        actors: item.catogary,
        englishTitle: item.ename,
        title: item.name,
        showInfo: item.release
      }
    })
    this.setData({
      movieList: res
    },() => {
      wx.hideLoading()
    })
  },
  // 取消
  cancel() {
    wx.navigateBack()
  }
})